#include "snake_move.h"

extern vector<Snake_cell> snake;

extern char map[15][15];

int snakeMove(int dx, int dy) {
	int head_x = snake[0].x, head_y = snake[0].y, len = snake.size();
	int tail_x = snake[len - 1].x, tail_y = snake[len - 1].y;
	int tar_x = head_x + dx, tar_y = head_y + dy, game = 1;

	for (int i = len - 1; i >= 1 ; i--) {
		snake[i] = snake[i - 1];
		map[snake[i].x][snake[i].y] = SNAKE_BODY;
	}

	map[tail_x][tail_y] = BLANK_CELL;

	if (map[tar_x][tar_y] == SNAKE_BODY || map[tar_x][tar_y] == WALL_CELL) {
		game = 0;
		map[tail_x][tail_y] = BLANK_CELL;
	}
	else {
		snake[0].x = tar_x;
		snake[0].y = tar_y;

		if (map[tar_x][tar_y] == SNAKE_FOOD) {
			snake.push_back(Snake_cell(tail_x, tail_y));
			map[tail_x][tail_y] = SNAKE_BODY;
			put_money();
		}
		else {
			map[tail_x][tail_y] = BLANK_CELL;
		}

	}
	map[tar_x][tar_y] = SNAKE_HEAD;

	output_map();

	return game;
}